/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.oa.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.legendshop.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.legendshop.framework.controller.BaseController;
import com.legendshop.oa.constants.PathResolver;
import com.legendshop.oa.dto.ProjectReport;
import com.legendshop.oa.dto.ProjectReportQueryDto;
import com.legendshop.oa.exception.BusinessException;
import com.legendshop.oa.model.Project;
import com.legendshop.oa.pages.AdminTilesPage;
import com.legendshop.oa.security.UserDetail;
import com.legendshop.oa.security.UserManager;
import com.legendshop.oa.service.ProjectService;
import com.legendshop.util.AppUtils;
import com.legendshop.util.Arith;

/**
 * The Class ProjectMemberController 控制器
 */
@Controller
@RequestMapping("/admin/projectReport")
public class ProjectReportController extends BaseController {

	@Autowired
	private ProjectService projectService;

	/**
	 * 列表查询
	 */
	@RequestMapping("/query/{projectId}")
	public String query(HttpServletRequest request, HttpServletResponse response, @PathVariable Long projectId, ProjectReportQueryDto projectReportQueryDto) {
		if (AppUtils.isBlank(projectId)) {
			throw new BusinessException("Project id can not be null while loading project members");
		}
		UserDetail userDetail = UserManager.getUser(request);
		projectReportQueryDto.setProjectId(projectId);
		projectReportQueryDto.setUserDetail(userDetail);
		Project project = projectService.getProject(userDetail, projectId);
		request.setAttribute("project", project);
		request.setAttribute("projectReportQueryDto", projectReportQueryDto);
		List<ProjectReport> reportList = projectService.getProjectReport(projectReportQueryDto);
		Double totalConsumedHours = 0d;
		if(AppUtils.isNotBlank(reportList)) {
			for (ProjectReport projectReport : reportList) {
				if(AppUtils.isNotBlank(projectReport.getTotalConsumed())) {
					totalConsumedHours = Arith.add(totalConsumedHours, projectReport.getTotalConsumed());
				}
				if(AppUtils.isBlank(projectReport.getUserRealName())) {
					projectReport.setUserRealName(projectReport.getUserName());
				}
			}
		}
		request.setAttribute("reportList", reportList);
		request.setAttribute("totalConsumedHours", new BigDecimal(totalConsumedHours).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue());



		List<String> names = reportList.stream().map(i -> i.getUserRealName()).collect(Collectors.toList());
		List<Double> consumed = reportList.stream().map(i -> new BigDecimal(i.getTotalConsumed()).setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue()).collect(Collectors.toList());

		request.setAttribute("names", JSONUtil.getJson(names));
		request.setAttribute("consumed", JSONUtil.getJson(consumed));


		return PathResolver.getPath(request, response, AdminTilesPage.PROJECT_REPORT_LIST_PAGE);
	}

	

}
